package ysq.xyg.recovery.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.controller.BaseController;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.EasyPoiUtil;
import ysq.xyg.recovery.model.po.RecoverySack;
import ysq.xyg.recovery.model.vo.RecoverySackExportVo;
import ysq.xyg.recovery.model.vo.RecoverySackImportVo;
import ysq.xyg.recovery.model.vo.RecoverySackVo;
import ysq.xyg.recovery.service.RecoverySackService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 回收金模块-袋子controller层
 *
 * @author congcong tao
 * @date 2019-7-8 14:45:11
 */
@RestController
public class RecoverySackController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(RecoverySackController.class);
    @Resource
    private RecoverySackService recoverySackService;

    private RecoverySack recoverySack;

    /**
     * 分页查询
     *
     * @param recoverySackVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author congcong tao
     * @date 2019-7-8 14:45:11
     */
    @BaseLog(module = "回收金模块",className = "袋子",methods = "分页查询",businessType = BaseConstant.SYS_BUSINESS_TYPE_GET,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/recoverySack/page",method = RequestMethod.POST)
    public ReturnEntity page(RecoverySackVo recoverySackVo, int pageSize, int pageNum){
        ReturnEntity returnEntity = recoverySackService.page(recoverySackVo,pageSize,pageNum);
        return returnEntity;
    }

    /**
     * 跳转连接
     *
     * @param id 主键id
     * @author congcong tao
     * @date 2019-7-8 14:45:11
     */
    @RequestMapping(value = "/recoverySack/getEntity",method = RequestMethod.GET)
    public ModelAndView getEntity(String id){
        ModelAndView view = new ModelAndView("/recovery/recoverySackUpdateOrSave");
        if(StrUtil.isNotEmpty(id)){
            recoverySack = recoverySackService.selectByPrimaryKey(id);
            view.addObject("recoverySack", recoverySack);
        }
        return view;
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @author congcong tao
     * @date 2019-7-8 14:45:11
     */
    @BaseLog(module = "回收金模块",className = "袋子",methods = "删除",businessType = BaseConstant.SYS_BUSINESS_TYPE_DELETE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/recoverySack/delete")
    public ReturnEntity delete(HttpServletRequest request, String id){
        int info = recoverySackService.deleteByPrimaryKey(id);
        if (info <= 0) {
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 添加或修改
     *
     * @param recoverySackVo Vo对象
     * @author congcong tao
     * @date 2019-7-8 14:45:11
     */
    @BaseLog(module = "回收金模块",className = "袋子",methods = "添加或修改",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/recoverySack/addOrUpdate",method = RequestMethod.POST)
    public ReturnEntity addOrUpdate(HttpServletRequest request,RecoverySackVo recoverySackVo){
        return recoverySackService.addOrUpdate(request, recoverySackVo,acceptCurrentUser(request));
    }

    /**
     * 模板下载
     */
    @RequestMapping("/recoverySack/exportMould")
    public void exportMould(HttpServletResponse response){
        String nowDate = DateUtil.formatDate(DateUtil.date());
        // 导出操作
        List<RecoverySackExportVo> recoverySackImportVos = new ArrayList<RecoverySackExportVo>();
        RecoverySackExportVo recoverySackImportVo = new RecoverySackExportVo();
        recoverySackImportVo.setCardNo("001");
        recoverySackImportVo.setStatus("1");
        recoverySackImportVos.add(recoverySackImportVo);
        EasyPoiUtil.exportExcel(recoverySackImportVos, nowDate + "袋子展示", "袋子", RecoverySackExportVo.class, nowDate + "袋子展示.xls", response);
    }

    /**
     * 导入
     * @param request
     * @param file
     * @author congcong tao
     * @date 2019-07-10 05:37
     */
    @RequestMapping(value = "/recoverySack/importData",method = RequestMethod.POST)
    public ReturnEntity importData(HttpServletRequest request, @RequestParam MultipartFile file){
        // 数据
        List<RecoverySackImportVo> recoverySackVoList = EasyPoiUtil.importExcel(file, 1, 1, RecoverySackImportVo.class);
        Map<String, RecoverySackImportVo> map = new HashMap<String, RecoverySackImportVo>();
        if (recoverySackVoList != null && recoverySackVoList.size() > 0) {
            // 保存
            for (RecoverySackImportVo recoverySackImportVo : recoverySackVoList) {
                map.put(recoverySackImportVo.getCardNo(), recoverySackImportVo);
            }
        }
        if (map != null && map.size() > 0) {
            for (Map.Entry<String, RecoverySackImportVo> entry : map.entrySet()) {
                String cardNo = entry.getKey();
                RecoverySackVo recoverySackVo = new RecoverySackVo();
                recoverySackVo.setCardNo(cardNo);
                recoverySackService.addOrUpdate(request, recoverySackVo, acceptCurrentUser(request));
            }
        }
        return new ReturnEntity();
    }
}